entrada ::=  sql_lista
    | sql_lista ";"

sql_lista  ::=  sql_lista1
			| sql_lista ";" sql_lista1

sql_lista1  ::=  sql

sql  ::=  st_abort
        | st_alter_database
        | st_alter_domain
        | st_alter_function
        | st_alter_group
        | st_alter_index
        | st_alter_role #| st_alter_user
        | st_alter_schema
        | st_alter_sequence
        | st_alter_table
        | st_alter_trigger
        | st_alter_type
        | st_alter_view
        | st_analyze
        | st_begin
        | st_checkpoint
        | st_cluster
        | st_comment # hasta aca anchu incluida
        | st_commit
        | st_commit_prepared
        | st_create_database
        | st_create_domain
        | st_create_function
        | st_create_index
        | st_create_role #| st_create_user | st_create_group
        | st_create_schema
        | st_create_sequence
        | st_create_table
        | st_create_table_as
        | st_create_trigger
        | st_create_type
        | st_create_view
        | st_delete
        | st_discard
        | st_drop_database # cristian inlcuida
        | st_drop_domain
        | st_drop_function
        | st_drop_group
        | st_drop_index
        | st_drop_owned_by
        | st_drop_role
        | st_drop_schema
        | st_drop_sequence
        | st_drop_table
        | st_drop_trigger
        | st_drop_type
        | st_drop_user
        | st_drop_view
        | st_execute
        | st_explain
        | st_grant
        | st_insert_into # hasta aca pato inlcuida
        | st_lock
        | st_reindex
        | st_release_savepoint
        | st_reset
        | st_revoke
        | st_rollback
        | st_rollback_to_savepoint
        | st_savepoint
        | st_select
        | st_select_into
        | st_set
        | st_set_constraints
        | st_set_role
        | st_set_transaction
        | st_start_transaction
        | st_truncate
        | st_update
        | st_vacuum
        | st_values

st_abort  ::=  ABORT opt_work_trans

opt_work_trans  ::=  WORK
			     | TRANSACTION
                 |

st_alter_database  ::=  ALTER DATABASE st_identificador opt_alter_database_options

opt_alter_database_options ::= opt_with CONNECTION LIMIT NUMERO_ENTERO
                    | RENAME TO st_identificador
					| OWNER TO st_identificador
					| SET st_configuration_parameter st_equal_to st_config_par_DEFAULT
					| SET st_configuration_parameter FROM CURRENT
					| RESET st_configuration_parameter
					| RESET ALL
                    |

opt_with  ::=  WITH
            |

st_configuration_parameter  ::= st_identificador

st_equal_to  ::=  TO
            | "="

st_config_par_DEFAULT  ::=  st_config_par_value
                        | DEFAULT

st_config_par_value  ::=  ON
                    | OFF
                    | TRUE
                    | FALSE
                    | YES
                    | NO
                    | NUMERO_ENTERO # 0 o 1

st_alter_domain  ::=  ALTER DOMAIN st_name st_alter_domain_opt

st_alter_domain_opt ::= st_set_drop_default
					| st_set_drop NOT NULL
					| ADD st_identificador #domain_constraint
					| DROP CONSTRAINT st_identificador opt_cascade_restrict
					| OWNER TO st_identificador
					| SET SCHEMA st_identificador

st_set_drop_default  ::=  SET DEFAULT scalar_exp
					| DROP DEFAULT

st_set_drop  ::=  SET
					| DROP

st_alter_function  ::=  ALTER FUNCTION st_name "(" opt_function_args ")" st_alter_function_opt

st_alter_function_opt ::= st_alter_function_action opt_restrict
					| st_set_schma_rname_ownr st_identificador


opt_function_args  ::=  function_args
                    |

function_args  ::= st_argmode st_argname st_argtype
                    | function_args "," st_argmode st_argname st_argtype

st_alter_function_action  ::=  st_strict
                            | st_volatility
							| opt_external st_security
							| COST NUMERO_DECIMAL
							| ROWS NUMERO_ENTERO
							| SET st_configuration_parameter st_equal_to st_config_par_DEFAULT
							| SET st_configuration_parameter FROM CURRENT
							| RESET st_configuration_parameter
							| RESET ALL

st_strict  ::=  CALLED ON NULL INPUT
							| RETURNS NULL ON NULL INPUT
							| STRICT

st_volatility  ::=  IMMUTABLE
							| STABLE
							| VOLATILE

st_security  ::=  SECURITY INVOKER
                | SECURITY DEFINER

opt_external  ::=  EXTERNAL
                |

opt_restrict  ::=  RESTRICT
                |

st_argmode  ::=  IN
			| OUT
			| INOUT
			| VARIADIC
            |


st_argname  ::=  st_identificador
            |

st_argtype  ::=  data_type_ref

st_alter_group  ::=  ALTER GROUP st_identificador st_alter_group_opt

st_alter_group_opt ::= ADD USER st_lista_id
				| DROP USER st_lista_id
				| RENAME TO st_identificador

st_alter_index  ::=  ALTER INDEX st_name st_alter_index_opt

st_alter_index_opt ::= RENAME TO st_identificador
				| SET "(" st_lista_id_equals ")"
				| RESET "(" st_lista_id ")"

st_cadena_entero  ::=  CADENA
                    | NUMERO_ENTERO

st_lista_id_equals  ::= st_identificador "=" st_cadena_entero
                        | st_lista_id_equals "," st_identificador "=" st_cadena_entero

st_alter_role  ::=  ALTER st_user_role_group st_identificador st_alter_role_opt

st_alter_role_opt ::= st_alter_role_atr
				| RENAME TO st_identificador
				| OWNER TO st_identificador
				| SET st_configuration_parameter st_equal_to st_config_par_DEFAULT
				| SET st_configuration_parameter FROM CURRENT
				| RESET st_configuration_parameter
				| RESET ALL

st_user_role_group  ::=  ROLE
                | USER
                | GROUP

st_alter_role_atr ::=  opt_with st_lista_alter_role_options
				|

st_lista_alter_role_options  ::=  st_alter_role_option
                | st_lista_alter_role_options st_alter_role_option

st_alter_role_option  ::=  st_superuser
                        | st_createbd
                        | st_createrole
                        | st_createuser
                        | st_inherit
                        | st_login
						| CONNECTION LIMIT NUMERO_ENTERO
						| VALID UNTIL CADENA
						| opt_encrypted PASSWORD CADENA

st_superuser  ::=  SUPERUSER
                | NOSUPERUSER
st_createbd  ::=  CREATEDB
				| NOCREATEDB
st_createrole  ::=  CREATEROLE
				| NOCREATEROLE
st_createuser  ::=  CREATEUSER
				| NOCREATEUSER
st_inherit  ::=  INHERIT
                | NOINHERIT
st_login  ::=  LOGIN
				| NOLOGIN

opt_encrypted  ::=  ENCRYPTED
                    | UNENCRYPTED
					|

st_alter_schema  ::=  ALTER SCHEMA st_identificador st_rename_owner_to st_identificador

st_rename_owner_to ::= RENAME TO
                        | OWNER TO

st_alter_sequence  ::=  ALTER SEQUENCE st_name st_alter_sequence_opt

st_alter_sequence_opt ::= opt_increment opt_min_value opt_max_value opt_start opt_restart opt_cache opt_cycle opt_owned_by
                    | st_set_schma_rname_ownr st_identificador

st_set_schma_rname_ownr ::= st_rename_owner_to
                        | SET SCHEMA

opt_increment  ::=  INCREMENT opt_by NUMERO_ENTERO
                |
opt_by  ::=  BY
        |

opt_min_value ::= MINVALUE NUMERO_ENTERO
                | NO MINVALUE
                |

opt_max_value ::= MAXVALUE NUMERO_ENTERO
                | NO MAXVALUE
                |

opt_start ::= START opt_with NUMERO_ENTERO
            |

opt_restart ::= RESTART opt_with NUMERO_ENTERO
                    |

opt_cache ::= CACHE NUMERO_ENTERO
                        |

opt_owned_by ::= OWNED BY st_id_none
                |

st_alter_table  ::=  ALTER TABLE opt_only st_name opt_star st_alter_t1
                | ALTER TABLE st_name st_alter_t2

st_alter_t1 ::= st_alter_tactions_list
                | RENAME opt_column st_identificador TO st_identificador

st_alter_t2 ::= RENAME TO st_identificador
                    | SET SCHEMA st_identificador

opt_star  ::=  "*"
            |

opt_only  ::=  ONLY
    |

opt_column  ::=  COLUMN
    |

st_alter_table_actions  ::=  ADD opt_column st_identificador data_type_ref opt_column_constraint
                        | DROP opt_column st_identificador opt_cascade_restrict
                        | st_alter_column
                        | ADD st_table_constraint
                        | DROP CONSTRAINT st_identificador opt_cascade_restrict
                        | DISABLE TRIGGER opt_id_all_user
                        | ENABLE TRIGGER opt_id_all_user
                        | ENABLE REPLICA TRIGGER st_identificador
                        | ENABLE ALWAYS TRIGGER st_identificador
                        | CLUSTER ON st_identificador
                        | SET WITHOUT CLUSTER
                        | SET WITH OIDS
                        | SET WITHOUT OIDS
                        | SET "(" st_lista_id_equals ")"
                        | RESET "(" st_lista_id ")"
                        | INHERIT st_identificador
                        | NO INHERIT st_identificador
                        | OWNER TO st_identificador

st_alter_column ::= ALTER opt_column st_identificador st_alter_column_opt

st_alter_column_opt ::= opt_set_data TYPE data_type_ref opt_using_exp
                        | st_set_drop_default
                        | st_set_drop NOT NULL
                        | SET STATISTICS NUMERO_ENTERO
                        | SET STORAGE st_storage

st_alter_tactions_list  ::=  st_alter_table_actions
                            | st_alter_tactions_list "," st_alter_table_actions


opt_set_data  ::=  SET DATA
                |

opt_using_exp  ::=  USING scalar_exp
                        |

st_storage  ::=  PLAIN
                | EXTERNAL
                | EXTENDED
                | MAIN

opt_id_all_user  ::=  st_identificador
                        | ALL
                        | USER
                        |

st_alter_trigger  ::=  ALTER TRIGGER st_identificador ON st_identificador RENAME TO st_identificador

st_alter_type  ::=  ALTER TYPE st_name st_set_schma_rname_ownr st_identificador

st_alter_view  ::=  ALTER VIEW st_name st_alter_view_opt

st_alter_view_opt ::= ALTER opt_column st_identificador st_set_drop_default
                | st_set_schma_rname_ownr st_identificador

st_analyze  ::=  ANALYZE opt_verbose opt_analyze_table

opt_verbose  ::=  VERBOSE
                    |

opt_analyze_table  ::=  st_name opt_column_name
                    |

st_begin  ::=  BEGIN opt_work_trans st_transaction_mode_list

st_checkpoint  ::=  CHECKPOINT

st_cluster  ::=  CLUSTER opt_verbose opt_cluster_opt

opt_cluster_opt ::= st_name opt_cluster_using
                    |

opt_cluster_using  ::=  USING st_identificador #indexname
                    |

st_comment  ::=  COMMENT ON st_comment_opt IS CADENA

st_comment_opt  ::=  TABLE st_name
                    | COLUMN st_name #table_name.column.name
                    | CAST "(" st_identificador AS data_type_ref ")"
                    | CONSTRAINT st_name ON st_name
                    | CONVERSION st_name
                    | DATABASE st_name
                    | DOMAIN st_name
                    | FUNCTION st_name "(" opt_function_args ")"
                    | INDEX st_name
                    | ROLE st_name
                    | SCHEMA st_name
                    | SEQUENCE st_name
                    | TRIGGER st_name ON st_name
                    | TYPE st_name
                    | VIEW st_name

st_commit  ::=  COMMIT opt_work_trans

st_commit_prepared  ::=  COMMIT PREPARED st_cadena_entero #transaction_id

st_create_database  ::=  CREATE DATABASE st_identificador st_create_db_opt

st_create_db_opt  ::=  opt_with opt_owner opt_template opt_encoding opt_collate opt_type opt_conn_limit

opt_owner ::= OWNER opt_equal st_default_id
            |

opt_template ::= TEMPLATE opt_equal st_default_id
                |

opt_encoding ::= ENCODING opt_equal st_default_cadena
                |

opt_collate ::= LC_COLLATE opt_equal st_identificador
                |

opt_type ::= LC_CTYPE opt_equal st_identificador
            |

opt_conn_limit ::= CONNECTION LIMIT opt_equal st_default_entero
                |

st_default_id  ::=  DEFAULT
                |  st_identificador

st_default_cadena  ::=  DEFAULT
            |  CADENA

st_default_entero  ::=  DEFAULT
            |  NUMERO_ENTERO

opt_equal ::=  "="
            |

st_create_domain ::=  CREATE DOMAIN st_name opt_as data_type_ref opt_default_exp opt_constraint_exp

opt_as ::=  AS
         |

opt_default_exp ::=  DEFAULT scalar_exp
                |

opt_constraint_exp ::= opt_constraint st_not_null_check
                    |

st_not_null_check  ::=  NOT NULL
                    |  NULL
                    |  CHECK "(" scalar_exp ")"


st_create_function  ::=  CREATE opt_or_replace FUNCTION st_name "(" opt_create_function_arg ")" opt_returns st_create_function_opt_list

opt_or_replace  ::=  OR REPLACE
                |

opt_create_function_arg  ::=  st_create_function_arg_list
                        |

opt_default_equal  ::=  DEFAULT scalar_exp
                        |  "=" scalar_exp
                        |

st_create_function_arg_list  ::=  st_argmode st_argname st_argtype opt_default_equal
                        | st_create_function_arg_list "," st_argmode st_argname st_argtype opt_default_equal

opt_returns  ::=  RETURNS data_type_ref # data_type_ref tablename.columnname%TYPE
                |  RETURNS TABLE "(" st_lista_id_data_type ")"  #col_name col_type
                |

st_lista_id_data_type  ::=  st_identificador data_type_ref
                            |  st_lista_id_data_type "," st_identificador data_type_ref

st_create_function_opt  ::=  LANGUAGE st_identificador
                        | LANGUAGE CADENA
                        | WINDOW
                        | st_volatility
                        | st_strict
                        | EXTERNAL st_security # no se puede reducir external, aparece conflicto R/R
                        | st_security
                        | COST NUMERO_DECIMAL
                        | ROWS NUMERO_ENTERO
                        | SET st_configuration_parameter st_to_equal_from
                        | AS "$" "$" st_sql_lista "$" "$" #definition
                        | AS st_sql_lista #definition
##                        | AS CADENA st_sql_lista

st_to_equal_from  ::=  TO st_config_par_value
                    |  "=" st_config_par_value
                    |  FROM CURRENT

st_create_function_opt_list  ::= st_create_function_opt
                                | st_create_function_opt_list st_create_function_opt

st_sql_lista ::= sql_lista
                        |

st_create_index ::= CREATE opt_unique INDEX opt_concurrently st_identificador ON st_name opt_using "(" st_create_idx_opt ")" opt_with_parent opt_where_clause

opt_unique ::=  UNIQUE
                    |

opt_concurrently ::= CONCURRENTLY
                  |

opt_using ::= USING st_create_index_method
                        |

st_create_index_method ::= BTREE
                        | HASH
                        | GIST
                        | GIN

st_create_idx_opt ::= st_create_idx_opt1 opt_asc_desc opt_nulls
                   | st_create_idx_opt "," st_create_idx_opt1 opt_asc_desc opt_nulls

st_create_idx_opt1 ::= st_identificador
                    | "(" scalar_exp ")"
                    | function_call

opt_nulls ::= NULLS FIRST
            | NULLS LAST
            |

opt_with_parent ::= WITH "(" st_lista_id_equals ")"
                    |

st_create_role ::= CREATE st_user_role_group st_identificador opt_create_role_options

opt_create_role_options ::= opt_with st_create_role_opt_list
                            |

st_create_role_opt_list ::= st_create_role_opt
                        | st_create_role_opt_list "," st_create_role_opt

st_create_role_opt ::= st_alter_role_option
                        | st_create_role_options


st_create_role_options ::= IN ROLE st_identificador
                        | IN GROUP st_identificador
                        | ROLE st_identificador
                        | ADMIN st_identificador
                        | USER st_identificador

st_create_schema ::=  CREATE SCHEMA st_identificador opt_authorization opt_schema_element_list
                    | CREATE SCHEMA AUTHORIZATION st_identificador opt_schema_element_list

opt_authorization ::= AUTHORIZATION st_identificador
                    |
opt_schema_element_list ::= schema_element_list
                    |
schema_element_list ::= schema_element
                        | schema_element_list schema_element

schema_element ::= st_create_table
                | st_create_view
                | st_create_index
                | st_create_sequence
                | st_create_trigger
                | st_grant

st_create_sequence ::= CREATE opt_temporary_temp SEQUENCE st_name opt_increment opt_min_value opt_max_value opt_start opt_cache opt_cycle opt_owned_by

opt_temporary_temp  ::=  TEMPORARY
                    | TEMP
                    |

opt_cycle ::=  opt_no CYCLE
            |

opt_no ::=  NO
        |

st_create_table ::=  CREATE opt_create_table_type TABLE st_name "(" opt_create_table_column ")" st_create_table_inherits opt_oids opt_on_commit


opt_create_table_type ::=  opt_global_local st_temporary_temp
                        |

opt_global_local ::=  GLOBAL
                    | LOCAL
                    |

st_temporary_temp ::= TEMPORARY
                    | TEMP

opt_create_table_column ::= st_create_table_opt_list
                            |

st_create_table_opt_list ::= st_create_table_opt
                            | st_create_table_opt_list "," st_create_table_opt


st_create_table_opt ::= st_identificador data_type_ref opt_default opt_column_constraint
                    |  st_table_constraint
                    |  st_like

opt_default ::=  DEFAULT st_default
            |

opt_column_constraint ::=  opt_constraint st_constraints_options opt_deferrable opt_init
                        |

opt_constraint ::= CONSTRAINT st_identificador
                    |

st_constraints_options ::= NOT NULL
                | NULL
                | UNIQUE st_index_parameter
                | UNIQUE
                | PRIMARY KEY st_index_parameter
                | PRIMARY KEY
                | CHECK "(" scalar_exp ")"
                | st_references

st_references ::= REFERENCES st_name opt_column_name opt_match opt_on_delete opt_on_update

st_index_parameter ::= WITH "(" st_index_parameter_1 ")" # index parameter

st_index_parameter_1 ::= st_identificador opt_equals_cad_num
                        | st_index_parameter_1 "," st_identificador opt_equals_cad_num

st_action ::= NO ACTION
            | RESTRICT
            | CASCADE
            | SET NULL
            | SET DEFAULT

opt_deferrable ::= DEFERRABLE
                | NOT DEFERRABLE
                |

opt_init ::= st_initially st_deferred_immediate
            |

st_initially ::= INITIALLY

st_deferred_immediate ::= DEFERRED
                            | IMMEDIATE

st_table_constraint ::= opt_constraint st_table_constraint_opt opt_deferrable opt_init

st_table_constraint_opt ::= UNIQUE "(" st_lista_id ")" st_index_parameter
                        | UNIQUE "(" st_lista_id ")"
                        | PRIMARY KEY "(" st_lista_id ")" st_index_parameter
                        | PRIMARY KEY "(" st_lista_id ")"
                        | CHECK "(" scalar_exp ")"
                        | FOREING KEY "(" st_lista_id ")" st_references

opt_match ::= MATCH FULL
            | MATCH PARTIAL
            | MATCH SIMPLE
            |

opt_on_delete ::= ON DELETE st_action
                |

opt_on_update ::= ON UPDATE st_action
                |

st_like ::= st_name st_including_excluding st_like_def_const_ind

st_including_excluding ::= INCLUDING
                | EXCLUDING

st_like_def_const_ind ::= DEFAULTS
                        | CONSTRAINT
                        | INDEXES

st_create_table_inherits ::= INHERITS "(" st_lista_name ")"
                            |

opt_oids ::= st_index_parameter
            | WITH OIDS
            | WITHOUT OIDS
            |

opt_on_commit ::= ON COMMIT st_on_commit_options
                |

st_on_commit_options ::= PRESERVE ROWS
                        | DELETE ROWS
                        | DROP

st_create_table_as  ::=  CREATE opt_create_table_type TABLE st_name opt_column_name opt_with_oids opt_on_commit AS st_as_query opt_data

opt_column_name ::= column_name
                    |

column_name ::= "(" st_lista_id ")"

opt_with_oids ::= WITH "(" st_create_table_as_oids_list ")"
                        | WITH OIDS
                        | WITHOUT OIDS
                        |

st_create_table_as_oids_list ::= st_identificador opt_equals_cad_num
                        | st_create_table_as_oids_list "," st_identificador opt_equals_cad_num

opt_equals_cad_num ::= "=" CADENA
                    | "=" NUMERO_ENTERO
                    | "=" NUMERO_DECIMAL
                    |

st_as_query ::=  st_select # Select
        | st_name # Tabla
        | st_values # Values
        | st_execute # Execute

opt_data ::= WITH opt_no DATA
            |

st_create_trigger  ::=  CREATE TRIGGER st_identificador st_before_after st_cte_trig_opt ON st_identificador opt_for EXECUTE PROCEDURE st_name "(" st_create_trigger_args ")" #nombre funcion, argumentos

st_before_after ::= BEFORE
                    | AFTER

st_cte_trig_opt ::=  INSERT
                    | UPDATE
                    | DELETE
                    | TRUNCATE
                    | st_cte_trig_opt OR INSERT
                    | st_cte_trig_opt OR UPDATE
                    | st_cte_trig_opt OR DELETE
                    | st_cte_trig_opt OR TRUNCATE

opt_for ::=  FOR opt_each st_row_statement
            |

opt_each ::=  EACH
            |

st_row_statement ::=  ROW
                    | STATEMENT

st_create_trigger_args ::= st_create_trigger_args_list # Argumentos de funcion
                        |

st_create_trigger_args_list ::= st_create_trigger_args_opt
                            | st_create_trigger_args_list "," st_create_trigger_args_opt

st_create_trigger_args_opt ::= CADENA
                            | NUMERO_ENTERO
                            | st_identificador # Ver si se pueden hacer llamadas a funciones aca

st_create_type  ::=  CREATE TYPE st_name opt_create_type_opt

opt_create_type_opt ::=  AS "("  st_lista_id_data_type ")"
                    | AS ENUM "(" st_lista_cadena ")"
                    | "(" INPUT "=" st_function "," OUTPUT "=" st_function opt_function ")"
                    |

st_lista_cadena ::=  CADENA
                    | st_lista_cadena "," CADENA

opt_function ::=  "," RECEIVE "=" st_function
                | "," SEND "=" st_function
                | "," TYPMOD_IN "=" st_function
                | "," TYPMOD_PUT "=" st_function
                | "," ANALYZE "=" st_function
                | "," INTERNALLENGTH "=" st_entero_var
                | "," PASSEDBYVALUE
                | "," ALIGNMENT "=" st_create_type_alignment
                | "," STORAGE "=" st_storage
                | "," LIKE "=" data_type_ref  # ver data_type_ref, puede ser un type creado
                | "," CATEGORY "=" CADENA  # solo un caracter 'U' por defecto
                | "," PREFERRED "=" st_true_false  # True or False. Default False
                | "," DEFAULT "=" st_default  # Default NULL
                | "," ELEMENT "=" data_type_ref
                | "," DELIMITER "=" CADENA  # solo un caracter
                | opt_function "," RECEIVE "=" st_function
                | opt_function "," SEND "=" st_function
                | opt_function "," TYPMOD_IN "=" st_function
                | opt_function "," TYPMOD_PUT "=" st_function
                | opt_function "," ANALYZE "=" st_function
                | opt_function "," INTERNALLENGTH "=" st_entero_var
                | opt_function "," PASSEDBYVALUE
                | opt_function "," ALIGNMENT "=" st_create_type_alignment
                | opt_function "," STORAGE "=" st_storage
                | opt_function "," LIKE "=" data_type_ref  # ver data_type_ref, puede ser un type creado
                | opt_function "," CATEGORY "=" CADENA  # solo un caracter 'U' por defecto
                | opt_function "," PREFERRED "=" st_true_false  # True or False. Default False
                | opt_function "," DEFAULT "=" st_default  # Default NULL
                | opt_function "," ELEMENT "=" data_type_ref
                | opt_function "," DELIMITER "=" CADENA  # solo un caracter


st_entero_var ::= NUMERO_ENTERO
                | VARIABLE

st_create_type_alignment ::= CHAR
                            | INT2
                            | INT4
                            | DOUBLE

st_true_false  ::=  TRUE
                    | FALSE

st_default ::= NUMERO_ENTERO
            | NUMERO_DECIMAL
            | CADENA
            | NULL

st_create_view ::=  CREATE opt_or_replace opt_temporary_temp VIEW st_name opt_column_name AS st_select_values

st_select_values ::= st_select
                    | st_values

st_delete ::=  DELETE FROM opt_only st_name opt_as_id opt_using_lista opt_where_current opt_returning

opt_using_lista ::=  USING st_lista_name
                |

opt_where_current ::=  WHERE search_condition
                    | WHERE CURRENT OF st_identificador
                    |

opt_returning ::=  RETURNING "*"
                    |  st_exp_as_id_list
                    |

st_exp_as_id_list ::= scalar_exp opt_as_output
                | st_exp_as_id_list "," scalar_exp opt_as_output

opt_as_output ::= AS st_identificador
                | st_identificador
                |

st_discard ::=  DISCARD st_discard_opt

st_discard_opt ::=  ALL
                | PLANS
                | st_temporary_temp

st_drop_database  ::=  DROP DATABASE opt_if_exist st_identificador

st_drop_domain  ::=  DROP DOMAIN opt_if_exist st_lista_name opt_cascade_restrict

opt_if_exist  ::=  IF EXIST
                |

opt_cascade_restrict  ::=  CASCADE
                        | RESTRICT
                        |

st_drop_function  ::=  DROP FUNCTION opt_if_exist st_name "(" function_args ")" opt_cascade_restrict

st_drop_group  ::=  DROP GROUP opt_if_exist st_lista_id

st_drop_index  ::=  DROP INDEX opt_if_exist st_lista_name opt_cascade_restrict

st_drop_owned_by  ::=  DROP OWNED BY opt_if_exist st_lista_id opt_cascade_restrict

st_drop_role  ::=  DROP ROLE opt_if_exist st_lista_id

st_drop_schema  ::=  DROP SCHEMA opt_if_exist st_lista_id opt_cascade_restrict

st_drop_sequence  ::=  DROP SEQUENCE opt_if_exist st_lista_name opt_cascade_restrict

st_drop_table  ::=  DROP TABLE opt_if_exist st_lista_name opt_cascade_restrict

st_drop_trigger  ::=  DROP TRIGGER opt_if_exist st_identificador ON st_name opt_cascade_restrict

st_drop_type  ::=  DROP TYPE opt_if_exist st_lista_name opt_cascade_restrict

st_drop_user  ::=  DROP USER opt_if_exist st_lista_id

st_drop_view  ::=  DROP VIEW opt_if_exist st_lista_name opt_cascade_restrict

st_execute  ::=  EXECUTE st_identificador opt_execute_param

opt_execute_param  ::=  "(" st_execute_param_list ")"
                    |

st_execute_param_list  ::=  st_execute_parametro
                        | st_execute_param_list "," st_execute_parametro

st_execute_parametro  ::=  st_identificador
                          | NUMERO_ENTERO
                          | NUMERO_DECIMAL

st_explain  ::=  EXPLAIN opt_analyze opt_verbose st_explain_opt

opt_analyze ::= ANALYZE
                |

st_explain_opt  ::=  st_select
                | st_insert_into
                | st_update
                | st_delete
                | st_values
                | st_execute
                | st_create_table_as

st_grant ::=  GRANT st_privileges ON st_table_id st_grant_fin
            | GRANT SELECT ON st_table_id st_grant_fin
            | GRANT st_privilege "," SELECT ON st_table_id st_grant_fin
            | GRANT UPDATE ON st_table_id st_grant_fin
            | GRANT st_privilege "," UPDATE ON st_table_id st_grant_fin
            | GRANT st_grant_opt ON st_table_id st_grant_fin
            | GRANT st_grant_using st_grant_fin
            | GRANT st_grant_create st_grant_fin
            | GRANT opt_exec_allon_allpriv st_grant_fin
            | GRANT st_grant_onschema st_grant_fin
            | GRANT opt_id_to st_grant_fin

st_privileges ::= st_privilege
            | st_all_privileges

st_all_privileges ::= ALL
            | ALL PRIVILEGES

st_privilege ::= INSERT
                | st_privilege "," INSERT
                | DELETE
                | st_privilege "," DELETE
                | TRUNCATE
                | st_privilege "," TRUNCATE
                | REFERENCES
                | st_privilege "," REFERENCES
                | TRIGGER
                | st_privilege "," TRIGGER

st_grant_opt ::= st_sel_ins_upd_ref
                | st_all_privileges column_name

st_sel_ins_upd_ref ::= SELECT column_name
                    | INSERT column_name
                    | UPDATE column_name
                    | REFERENCES column_name
                    | st_sel_ins_upd_ref "," SELECT column_name
                    | st_sel_ins_upd_ref "," INSERT column_name
                    | st_sel_ins_upd_ref "," UPDATE column_name
                    | st_sel_ins_upd_ref "," REFERENCES column_name

st_table_id ::= opt_table st_lista_id

opt_table ::= TABLE
            |

st_grant_using ::= st_select_update ON SEQUENCE st_lista_name
                | USAGE ON SEQUENCE st_lista_name
                | st_select_update "," USAGE ON SEQUENCE st_lista_name
                | st_all_privileges ON SEQUENCE st_lista_name

st_select_update ::= SELECT
                    | st_select_update "," SELECT
                    | UPDATE
                    | st_select_update "," UPDATE

st_grant_create ::= opt_con_temp ON DATABASE st_lista_id
                | CREATE ON DATABASE st_lista_id
                | opt_con_temp "," CREATE ON DATABASE st_lista_id
                | st_all_privileges ON DATABASE st_lista_id

opt_con_temp ::= CONNECT
                | opt_con_temp "," CONNECT
                | TEMPORARY
                | opt_con_temp "," TEMPORARY
                | TEMP
                | opt_con_temp "," TEMP

opt_exec_allon_allpriv ::= EXECUTE ON FUNCTION st_grant_id
                        | ALL ON FUNCTION st_grant_id
                        | ALL PRIVILEGES ON FUNCTION st_grant_id

st_grant_id ::= st_name "(" opt_function_args ")"
            | st_grant_id "," st_name "(" opt_function_args ")"

st_grant_onschema  ::= opt_create_usage ON SCHEMA st_lista_id
                    | st_all_privileges ON SCHEMA st_lista_id

opt_create_usage ::= CREATE
                    | opt_create_usage "," CREATE
                    | USAGE
                    | opt_create_usage "," USAGE

opt_id_to ::= st_lista_id TO st_lista_id opt_with_admin_option
            | st_lista_id TO st_lista_id

opt_with_admin_option ::= WITH ADMIN OPTION


st_grant_fin ::= TO opt_group_id_public opt_with_grant_option

opt_group ::= GROUP
            |

st_grant_fin1 ::= opt_group st_identificador
                | PUBLIC

opt_group_id_public ::= st_grant_fin1
                    | opt_group_id_public "," st_grant_fin1

opt_with_grant_option ::= WITH GRANT OPTION
                        |

st_insert_into ::=   INSERT INTO st_name opt_column_name st_def_val st_insert_into_returing

st_def_val ::=  DEFAULT VALUES
                    | VALUES "(" opt_def_val_list ")"
                    | st_select

opt_def_val_list ::=  "(" opt_escalar_default ")"
                    | opt_def_val_list "," "(" opt_escalar_default ")"

opt_escalar_default ::=  scalar_exp
                    | opt_def_val_list "," scalar_exp
                    | DEFAULT
                    | opt_def_val_list "," DEFAULT

st_insert_into_returing ::=  RETURNING "*"
                        | st_insert_into_id_list

st_insert_into_id_list ::=  st_identificador opt_as_id # Output_expression hace referencia a un nombre de columna
                        | st_insert_into_id_list "," st_identificador opt_as_id

st_lock ::=  LOCK opt_table opt_only st_lista_name opt_in_lock opt_nowait

opt_in_lock ::=  IN st_lock_mode MODE
            |

st_lock_mode ::= ACCESS SHARE
                | ROW SHARE
                | ROW EXCLUSIVE
                | SHARE UPDATE EXCLUSIVE
                | SHARE
                | SHARE ROW EXCLUSIVE
                | EXCLUSIVE
                | ACCESS EXCLUSIVE

opt_nowait ::=  NOWAIT
            |

st_reindex ::=  REINDEX st_reindex_opt st_identificador opt_force

st_reindex_opt ::=  INDEX
               | TABLE
               | DATABASE
               | SYSTEM

opt_force ::=  FORCE
               |

st_release_savepoint  ::=  RELEASE SAVEPOINT opt_savepoint st_identificador

opt_savepoint  ::=  SAVEPOINT
                          |

st_reset ::=  RESET st_configuration_parameter
            | RESET ALL

st_revoke ::= REVOKE opt_grant_option_for st_privileges ON st_table_id st_revoke_fin #1
            | REVOKE opt_grant_option_for SELECT ON st_table_id st_revoke_fin #2
            | REVOKE opt_grant_option_for st_privilege "," SELECT ON st_table_id st_revoke_fin#2
            | REVOKE opt_grant_option_for UPDATE ON st_table_id st_revoke_fin#2
            | REVOKE opt_grant_option_for st_privilege "," UPDATE ON st_table_id st_revoke_fin#2
            | REVOKE opt_grant_option_for st_grant_opt ON st_table_id st_revoke_fin #2
            | REVOKE opt_grant_option_for st_grant_using st_revoke_fin #3
            | REVOKE opt_grant_option_for st_grant_create st_revoke_fin #4
            | REVOKE opt_grant_option_for opt_exec_allon_allpriv st_revoke_fin #5
            | REVOKE opt_grant_option_for st_grant_onschema st_revoke_fin #6
            | REVOKE opt_admin_option_for opt_id_from #7

opt_grant_option_for ::= GRANT OPTION FOR
                        |

opt_admin_option_for ::= ADMIN OPTION FOR
                        |

opt_id_from ::= st_lista_id FROM st_lista_id opt_cascade_restrict

st_revoke_fin ::= FROM opt_group_id_public opt_cascade_restrict


st_rollback ::=  ROLLBACK opt_work_trans

st_rollback_to_savepoint  ::=  ROLLBACK opt_work_trans TO opt_savepoint st_identificador

st_savepoint ::=  SAVEPOINT st_identificador

st_select ::= SELECT opt_all_distinct st_select3 opt_from_clause opt_where_clause opt_group_by_clause opt_having_clause opt_sql_mode opt_order_by_clause opt_limit opt_offset opt_fetch opt_forupdate_forshare

opt_all_distinct ::= ALL
                 | DISTINCT opt_on_exp
                 |

opt_on_exp ::= ON "(" st_list_exp ")"
                |

st_list_exp ::=  scalar_exp # Each expression can be the name or ordinal number of an output column (SELECT list item), or it can be an arbitrary expression formed from input-column values.
                | st_list_exp "," scalar_exp

st_select3 ::= "*"
                 | st_exp_as_id_list

opt_from_clause ::= FROM st_from_item_list
                 |

st_from_item_list ::= st_from_item
                  | st_from_item_list "," st_from_item

opt_where_clause ::= WHERE search_condition
                 |

opt_group_by_clause ::= GROUP BY ordering_spec_commalist
                  |

ordering_spec_commalist ::= ordering_spec
                        | ordering_spec_commalist ',' ordering_spec

ordering_spec ::= scalar_exp opt_asc_desc

opt_asc_desc ::=
             | ASC
             | DESC
             | USING OPERATOR

opt_having_clause ::= HAVING st_search_condition_list
                  |

st_search_condition_list ::= search_condition
                  | st_search_condition_list "," search_condition

opt_sql_mode ::= UNION ALL SELECT
                  | UNION SELECT
                  | INTERSECT ALL SELECT
                  | INTERSECT SELECT
                  | EXCEPT ALL SELECT
                  | EXCEPT SELECT
                  |

opt_order_by_clause ::=
                    | ORDER BY ordering_spec_commalist opt_nulls

opt_limit ::= LIMIT NUMERO_ENTERO
                    | LIMIT ALL
                    |

opt_offset ::= OFFSET FIRST opt_entero st_rows ONLY
                   | OFFSET NEXT opt_entero st_rows ONLY
                   |

opt_entero ::= NUMERO_ENTERO
                |

st_rows ::= ROW
        | ROWS

opt_fetch ::= FETCH FIRST opt_entero st_rows ONLY
                  | FETCH NEXT opt_entero st_rows ONLY
                  |

opt_forupdate_forshare ::= FOR st_update_share opt_of_lista_id opt_nowait
                  |

st_update_share ::= UPDATE
                    | SHARE

opt_of_lista_id ::= OF st_lista_id
                    |

st_from_item  ::=  st_from_item1
                 | st_from_item2
                 | st_from_item4
                 | st_from_item5
                 | st_from_item6


st_from_item1  ::=  opt_only st_identificador opt_star opt_as_id_column_name

opt_as_id_column_name  ::= AS st_identificador opt_column_name # no se puede reducir el as porque aparece conflicto R/R
                    | st_identificador opt_column_name
                    |

st_from_item2  ::=  "(" st_select ")" opt_as st_identificador opt_column_name

st_from_item4  ::=  st_identificador "(" opt_lista_id ")" opt_as st_identificador opt_column_name

opt_lista_id ::= st_lista_id
                |

st_from_item5  ::=  st_identificador "(" opt_lista_id ")" AS  "(" st_lista_id ")"

st_from_item6  ::=  st_from_item opt_natural st_join_type st_from_item st_from_item6_2

opt_natural  ::=  NATURAL
                    |

st_from_item6_2  ::=  ON scalar_exp
                    | USING "(" st_lista_id ")"

st_join_type  ::=  INNER JOIN
                 | JOIN
                 | LEFT OUTER JOIN
                 | LEFT JOIN
                 | RIGHT OUTER JOIN
                 | RIGHT JOIN
                 | FULL OUTER JOIN
                 | FULL JOIN
                 | CROSS JOIN

st_select_into ::= SELECT opt_all_distinct st_select3 INTO opt_temporary_temp opt_table st_identificador opt_from_clause opt_where_clause opt_group_by_clause opt_having_clause opt_sql_mode opt_order_by_clause opt_limit opt_offset opt_fetch opt_forupdate_forshare

st_set  ::=  SET opt_local_session st_set2

opt_local_session  ::=  SESSION
            | LOCAL
            |

st_set2  ::=  st_configuration_parameter st_equal_to st_set2_2
            | TIME ZONE st_set2_3

st_set2_2 ::= st_identificador
    | CADENA
    | DEFAULT
    | NUMERO_ENTERO
    | OFF
    | TRUE
    | FALSE
    | PUBLIC
    | st_set2_2 "," st_identificador
    | SCHEMA

st_set2_3  ::=  st_time_zone
              | LOCAL
              | DEFAULT

st_time_zone ::= NUMERO_ENTERO #timezone # EJ: Europe/Rome = italy / -7  / INTERVAL ?-08:00? HOUR TO MINUTE

st_set_constraints ::= SET CONSTRAINTS st_all_lista_id st_deferred_immediate

st_all_lista_id ::=  ALL
                      | st_lista_id

st_set_role  ::=  SET opt_local_session ROLE st_id_none
                | RESET ROLE

st_id_none  ::=  st_identificador
                   | NONE

st_set_transaction ::= SET TRANSACTION st_transaction_mode

st_transaction_mode ::=  ISOLATION LEVEL st_trans_mode
                        | st_read_write_only

st_read_write_only ::= READ WRITE
                        | READ ONLY

st_trans_mode ::=  SERIALIZABLE
                        | REPEATABLE READ
                        | READ COMMITTED
                        | READ UNCOMMITTED

st_start_transaction ::= START TRANSACTION st_transaction_mode_list

st_transaction_mode_list ::= st_transaction_mode
                            | st_transaction_mode_list "," st_transaction_mode

st_truncate  ::=  TRUNCATE opt_table opt_only st_lista_name opt_restart_continue_identity opt_cascade_restrict

opt_restart_continue_identity  ::=  RESTART IDENTITY
                 | CONTINUE IDENTITY
                 |

st_update ::=  UPDATE opt_only st_name opt_as_id SET st_update_set opt_from_lista_id opt_where_current opt_returning

opt_as_id ::= AS st_identificador
            | st_identificador
            |

st_update_set ::=  st_identificador "=" st_exp_default
             | st_update_set "," st_identificador "=" st_exp_default
             | "(" st_lista_id ")" "=" "(" st_list_exp_default ")"
             | st_update_set "," "(" st_lista_id ")" "=" "(" st_list_exp_default ")"

st_exp_default ::= scalar_exp
                | DEFAULT

st_list_exp_default ::=  scalar_exp
                | st_list_exp_default "," scalar_exp
                | DEFAULT
                | st_list_exp_default "," DEFAULT

opt_from_lista_id ::= FROM st_lista_id
            |

st_vacuum  ::=  VACUUM opt_full opt_freeze opt_verbose st_vacuum_exp

opt_full ::= FULL
            |

opt_freeze ::= FREEZE
            |

st_vacuum_exp  ::=  opt_st_name
               | ANALYZE opt_p_st_name

opt_st_name  ::=  st_name
                 |

opt_p_st_name  ::=  st_name
                 | st_name "(" st_lista_id ")"
                 |

st_values ::= VALUES st_p_list_exp opt_order_by_clause opt_limit opt_offset_start opt_fetch

st_p_list_exp ::= "(" st_list_exp ")"
                | st_p_list_exp "," "(" st_list_exp ")"

opt_offset_start ::=  OFFSET NUMERO_ENTERO opt_row_rows
                |

opt_row_rows ::= ROW
                | ROWS
                |

st_data_type  ::= BIGINT
                | INT8
                | BIGSERIAL
               	| SERIAL8
                | BIT st_data_type_1
                | BIT VARYING st_data_type_1
               	| VARBIT
                | BOOLEAN
                | BOOL
                | BOX
                | BYTEA
                | CHARACTER VARYING st_data_type_1
               	| VARCHAR st_data_type_1
                | CHARACTER st_data_type_1
                | CHAR st_data_type_1
                | CIDR
                | CIRCLE
                | DATE
                | DOUBLE PRECISION
               	| FLOAT8
                | INET
                | INTEGER
                | INT
                | INT4
                | LINE
                | LSEG
                | MACADDR
                | MONEY
                | NUMERIC st_data_type_2
                | DECIMAL st_data_type_2
                | PATH
                | POINT
                | POLYGON
                | REAL
                | FLOAT4
                | SMALLINT
                | INT2
                | SERIAL
                | SERIAL4
                | TEXT
                | TIME
                | TIMETZ
                | TIMESTAMP
                | TIMESTAMPTZ
                | TSQUERY
                | TSVECTOR
                | TXID_SNAPSHOT
                | UUID
                | XML

st_data_type_1  ::=  "(" NUMERO_ENTERO ")"
                    |

st_data_type_2  ::=  "(" NUMERO_ENTERO "," NUMERO_ENTERO ")"
                    |

array_modifier ::= ARRAY
               | ARRAY '[' NUMERO_ENTERO ']'
               | '[' NUMERO_ENTERO ']'
               | '[' ']'

data_type_ref ::= data_type_ref array_modifier
              | st_data_type
              | st_name

st_name  ::= st_name "." st_identificador
    | st_identificador
    | PUBLIC "." st_identificador

st_function ::= st_name

st_lista_id ::= st_identificador
    | st_lista_id "," st_identificador

st_lista_name  ::=  st_name
                | st_lista_name "," st_name

# Desde aca lo nuevo

search_condition ::= search_condition OR search_condition
                | search_condition AND search_condition
                | NOT search_condition
                | '(' search_condition ')'
                | predicate

predicate ::= comparison_predicate
            | between_predicate
            | like_predicate
            | test_for_null
            | in_predicate
            | all_or_any_predicate
            | existence_test
            | scalar_exp_predicate

comparison_predicate ::= scalar_exp COMPARISON scalar_exp

between_predicate ::= scalar_exp NOT BETWEEN scalar_exp AND scalar_exp
                 | scalar_exp BETWEEN scalar_exp AND scalar_exp

like_predicate ::= scalar_exp NOT LIKE scalar_exp
                | scalar_exp LIKE scalar_exp

test_for_null ::= scalar_exp IS NOT NULL
                | scalar_exp IS NULL
                | scalar_exp ISNULL
                | scalar_exp NOTNULL

in_predicate ::= scalar_exp NOT IN subquery
             | scalar_exp IN subquery
             | scalar_exp NOT IN '(' scalar_exp_commalist ')'
             | scalar_exp IN '(' scalar_exp_commalist ')'

all_or_any_predicate ::= scalar_exp COMPARISON any_all_some subquery

any_all_some ::= ANY
             | ALL
             | SOME

existence_test ::= EXISTS subquery

scalar_exp_predicate ::= scalar_exp

subquery ::= '(' st_select ')'

scalar_exp ::= scalar_exp '-' scalar_exp
           | scalar_exp '+' scalar_exp
           | scalar_exp '*' scalar_exp
           | scalar_exp '/' scalar_exp
           | '+' scalar_exp
           | '-' scalar_exp
           | assignment_statement
           | string_concatenation_operator
           | column_ref
           | scalar_exp_no_col_ref
##           | obe_literal

assignment_statement ::= lvalue EQUALS scalar_exp
                    | column_ref '[' scalar_exp ']' EQUALS scalar_exp

lvalue ::= column_ref

column_ref ::= st_name

string_concatenation_operator ::= scalar_exp BARRA scalar_exp # operador de concat string probar!

scalar_exp_no_col_ref ::= aggregate_ref
                      | scalar_subquery
                      | '(' scalar_exp ')'
                      | '(' scalar_exp ',' scalar_exp_commalist ')'
                      | function_call
                      | new_invocation
                      | cvt_exp
                      | cast_exp
                      | simple_case
                      | searched_case
                      | coalesce_exp
                      | nullif_exp
                      | array_ref
##                      | static_method_invocation
##                      | method_invocation

aggregate_ref ::= AGGREGATE st_name '(' opt_arg_commalist ')'

scalar_subquery ::= subquery

function_call ::= st_name '(' opt_arg_commalist ')'
              | EXTRACT '(' st_name FROM scalar_exp ')'
              | CALL '(' scalar_exp ')' '(' opt_arg_commalist ')'
              | CURRENT_DATE
              | CURRENT_TIME
              | CURRENT_TIMESTAMP


##method_identifier ::= st_identificador
##                  | EXTRACT

opt_arg_commalist ::=
                  | scalar_exp_commalist
                  | scalar_exp_commalist ',' as_commalist
                  | as_commalist

scalar_exp_commalist ::= scalar_exp
                     | scalar_exp_commalist ',' scalar_exp

as_commalist ::= as_expression
             | as_commalist ',' as_expression
             | as_commalist ',' scalar_exp

as_expression ::= scalar_exp AS st_identificador data_type_ref
              | scalar_exp AS st_identificador
              | scalar_exp st_identificador

new_invocation ::= NEW st_name '(' opt_arg_commalist ')'

cvt_exp ::= CONVERT '(' data_type_ref ',' scalar_exp ')'

cast_exp ::= CAST '(' scalar_exp AS data_type_ref opt_collate_exp ')'

opt_collate_exp ::=
                | COLLATE st_name # q_table_name

simple_case ::= CASE scalar_exp simple_when_list END

simple_when_list ::= simple_when
                | simple_when_list simple_when

simple_when ::= WHEN scalar_exp THEN scalar_exp
            | ELSE scalar_exp

searched_case ::= CASE searched_when_list END

searched_when_list ::= searched_when
                   | searched_when_list searched_when

searched_when ::= WHEN search_condition THEN scalar_exp
              | ELSE scalar_exp

coalesce_exp ::= COALESCE '(' scalar_exp_commalist ")"

nullif_exp ::= NULLIF '(' scalar_exp ',' scalar_exp ")"

array_ref ::= scalar_exp_no_col_ref '[' scalar_exp "]"
          | lvalue_array_ref

lvalue_array_ref ::= column_ref '[' scalar_exp ']'

st_identificador ::= IDENTIFICADOR

# Ver que va en argumentos de funcion de un trigger


